/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.ui;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;

import id.duapuluhdua.dakidaki.common.CommonActivity;
import id.duapuluhdua.dakidaki.map.Screen;

/**
 *
 */
public class UIScreen extends UIElement
{
  /** Constants */
  private static final String TAG = "UIScreen";
  private static final int UI_VISIBLE_DURATION = 3000;

  /** Fields */
  private ArrayList<UIElement> mVecElement;

  private long mUserInterfaceVisibleTime;

  private static Boolean bUserInterfaceVisible = false;

  /**
   * The default constructor.
   *
   * @param parent
   * @param screen
   */
  public UIScreen(CommonActivity parent, Screen screen)
  {
    super(parent, screen);

    mVecElement = new ArrayList<UIElement>();
    mVecElement.add(new DigitDisplayDistance(parent, screen));
    mVecElement.add(new DigitDisplayAltitude(parent, screen));
  }

  @Override
  public void doDraw(Canvas canvas)
  {
    UIElement element = null;

    final boolean bVisible = isUserInterfaceVisible();

    // Draw permanent elements first
    for (int i = 0; i < mVecElement.size(); i++) {
      element = mVecElement.get(i);
      if (element.bAlwaysVisible) {
        element.doDraw(canvas);
      }
    }

    // Then draw toggle elements
    for (int i = 0; i < mVecElement.size(); i++) {
      element = mVecElement.get(i);
      if (!element.bAlwaysVisible) {
        if (bVisible) {
          element.doDraw(canvas);
        }
      }
    }
  }

  /**
   * @param width
   * @param height
   * @param screenOrientation
   */
  @Override
  public void surfaceSizeChanged(int width, int height, int screenOrientation)
  {
    UIElement.screenWidth = width;
    UIElement.screenHeight = height;

    for (int i = 0; i < mVecElement.size(); i++) {
      mVecElement.get(i).surfaceSizeChanged(width, height, screenOrientation);
    }
  }

  /**
   * @param event
   * @return
   */
  public boolean onTouchEvent(MotionEvent event)
  {
    if (isUserInterfaceVisible()) {
      if ((event.getAction() == MotionEvent.ACTION_DOWN)
          || (event.getAction() == MotionEvent.ACTION_MOVE)) { // Touch
        for (int i = 0; i < mVecElement.size(); i++) {
          final UIElement element = mVecElement.get(i);
          if (element.isInTouchArea(event)) {
              element.setPressed();
              return true;
          }
        }
      }

      if (event.getAction() == MotionEvent.ACTION_UP) { // Release the touch
        for (int i = 0; i < mVecElement.size(); i++) {
          final UIElement element = mVecElement.get(i);
          final boolean bPressed = element.isPressedDown();

          element.clearPressed();

          if (bPressed && element.isInTouchArea(event)) {
            element.doClick();
            return true;
          }
        }
      }
    }
    return false;
  }

  public static boolean isUserInterfaceVisible()
  {
    synchronized(bUserInterfaceVisible)
    {
      return bUserInterfaceVisible;
    }
  }

  /**
   * @see id.duapuluhdua.dakidaki.ui.UIElement#onClick()
   */
  @Override
  public void onClick()
  {
    Log.i(TAG, "A user clicks the screen.");
  }

  /**
   *
   */
  @Override
  public void updateObjectsState()
  {
    final boolean bVisible = isUserInterfaceVisible();

    for (int i = 0; i < mVecElement.size(); i++) {
      mVecElement.get(i).updateObjectsState();
    }

    if (bVisible) {
      long currentDuration = System.currentTimeMillis() - mUserInterfaceVisibleTime;
      if (currentDuration >= UI_VISIBLE_DURATION) {
        hideUserInterface();
      }
    }
  }

  /**
   *
   */
  public void showUserInterface()
  {
    synchronized(bUserInterfaceVisible)
    {
      bUserInterfaceVisible = true;
    }
    mUserInterfaceVisibleTime = System.currentTimeMillis();
  }

  /**
   *
   */
  private void hideUserInterface()
  {
    synchronized(bUserInterfaceVisible)
    {
      bUserInterfaceVisible = false;
    }
  }
}
